package cn.xerllent.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MysqlMetaObjectHandler implements MetaObjectHandler{
    /**
     * 测试 user 表 name 字段为空自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println("*************************insert of mysql fill*************************");
        // 测试下划线
//        Object createtime = this.getFieldValByName("createtime", metaObject);
//        if (createtime == null) {
//            //测试实体没有的字段，配置在公共填充，不应该set到实体里面
//            this.strictInsertFill(metaObject, "createtime", LocalDateTime.class, LocalDateTime.now());
//        }
//        Object updatetime = this.getFieldValByName("updatetime", metaObject);
//        if (updatetime == null) {
//            //测试实体没有的字段，配置在公共填充，不应该set到实体里面
//            this.strictInsertFill(metaObject, "updatetime", LocalDateTime.class, LocalDateTime.now());
//        }
        
        this.strictInsertFill(metaObject, "createtime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatetime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("*************************update of mysql fill*************************");
        //测试实体没有的字段，配置在公共填充，不应该set到实体里面
        this.strictUpdateFill(metaObject, "updatetime", LocalDateTime.class, LocalDateTime.now());
    }
    

}
